
var util = require('../../utils/util.js')
var tool = require('../../utils/rjTool.js')
Page({
	data:{
		    //设备参数
        SCREEN_WIDTH: 0,
        SCREEN_HEIGHT: 0,
		    deviceModle:{},
        startPoint: {x:0,y:0}, //色温灯触摸
        animationData: {},
        startAngle: 0,
        kgOn: false,  //开关
        isOnline:false,
        stateArr:{},
        hasHuxiDeng:false,
        isBF:false,
        bfTitle:"撤",
        rotateAngle: 0,
        isShowBottom:false,
        arrowRotate: 180,
        bottomViewHeight:{default:80,three:390,four:490,five:590},
        btHeight: 80, //总高度
        listHeight:0, //列表高度
        trapHeight:-6,   //下陷深度
        animationData:{}

	},
	onLoad:function(options){


		var jsonData = JSON.parse(options["deviceModle"])

		//是否在线
		var isOnline = false


        //有呼吸灯
		var  hasHuxiDeng = false
        
        //ctrlArr
        var stateArr = []


     var state = jsonData.state
     stateArr = state.split("|")

     if (stateArr.length>1) {
     	hasHuxiDeng = true
     }

     //控制字符串转模型数组
     util.mapCtrlArray(stateArr,function(la){

     	stateArr = la
     })
    
     //判断在线否
		if (jsonData["onlineMode"] == 1) {
             isOnline = true
		}else{
             isOnline = false
		}
    
     var isBF = false
     var bfTitle = "撤"

    //门磁判断
    if (jsonData["deviceTypeId"] === '0014') {
         
        if (stateArr[0].state === '1') {
             isBF = true
             bfTitle= "布"
        }
    }
    
    console.log("isBF:" + isBF)
    console.log(jsonData)
    console.log(stateArr)

   //转盘
    // this.animation = wx.createAnimation({
    //   duration: 0.0000001,
    //  timingFunction: 'ease',
    // })
    // this.animation.rotate(this.data.startAngle).step()

     var btHeight = this.data.bottomViewHeight.default;
  

    //bottView
    var type = jsonData["deviceTypeId"]

     if (type === '0014' || type === '0012') {
         btHeight  = this.data.bottomViewHeight.three
     }

      if (type === '0018') {

      btHeight  = this.data.bottomViewHeight.four
     }
     
     var listHeight = btHeight - 80
     
     var trapHeight =   -(listHeight)-6



   console.log('btHeight: ' + btHeight)

		this.setData({
			deviceModle: jsonData,
			isOnline: isOnline,
			stateArr: stateArr,
			hasHuxiDeng: hasHuxiDeng,
      isBF: isBF,
      btHeight: btHeight,
      listHeight:listHeight,
      trapHeight:trapHeight,
      bfTitle: bfTitle
		})


	},
	onReady: function (e) {
    // 使用 wx.createContext 获取绘图上下文 context
    // 
    // 
    var animation = wx.createAnimation({
        duration: 250,
       timingFunction: "ease",
        delay: 0
       })
     this.animation = animation
    var ctx = wx.createCanvasContext('canvas-id-bg')
  
     // Draw coordinates
     ctx.arc(117.5, 117.5, 103, -(5/4 *Math.PI), (1/4*Math.PI))
     
     ctx.setFillStyle('rgba(1, 1, 1, 0.0)')
     ctx.setStrokeStyle('#FFFFFF')
     ctx.setLineCap('round')
     ctx.setLineWidth(12)
     ctx.fill()
     ctx.stroke()
     ctx.draw()

    

    // context.setStrokeStyle("#00ff00")
    // context.setLineWidth(5)
    // context.rect(0, 0, 200, 200)
    // context.stroke()
    // context.setStrokeStyle("#ff0000")
    // context.setLineWidth(2)
    // context.moveTo(160, 100)
    // context.arc(100, 100, 60, 0, 2 * Math.PI, true)
    // context.moveTo(140, 100)
    // context.arc(100, 100, 40, 0, Math.PI, false)
    // context.moveTo(85, 80)
    // context.arc(80, 80, 5, 0, 2 * Math.PI, true)
    // context.moveTo(125, 80)
    // context.arc(120, 80, 5, 0, 2 * Math.PI, true)
    // context.stroke()
    // context.draw()
    // 
      
   //获取屏幕宽高
  var  SCREEN_WIDTH  = 0.0
  var  SCREEN_HEIGHT = 0.0
  
  util.getSystemWHInfo(function(width,height){
      
          SCREEN_WIDTH = width
          SCREEN_HEIGHT = height

  })


  this.setData({
      animationData: this.animation.export(),
      SCREEN_WIDTH: SCREEN_WIDTH,
      SCREEN_HEIGHT: SCREEN_HEIGHT
    })


  },
	onShow:function(){
       //设置导航栏标题
		wx.setNavigationBarTitle({
           title: this.data.deviceModle["deviceName"]
         })
	},

	onHide:function(){
		
	},
	onUnload:function(){
		
	},
	onPullDownRefresh:function(){
		
	},
	onReachBottom:function(){
		
	},
	tapOn:function(e){
       
      var model = this.data.deviceModle
      var kgOn = this.data.kgOn
      var obj = this.data.stateArr[this.data.stateArr.length -1]
     
      var ctrl = "num:" + obj.num + ",state:1"
     //  console.log(ctrl)
     // console.log(model.deviceId)
       
       util.ctrlControl(model.deviceId,ctrl,function(success,msg){
         
          if (!success) {
          
            console.log(msg)

          }

       })




       this.setData({
          KgOn:true,
          isBF:true,
          bfTitle:"布"
       })
	},
	tapOff:function(e){
      var model = this.data.deviceModle
      var kgOn = this.data.kgOn
      var obj = this.data.stateArr[this.data.stateArr.length -1]
     
      var ctrl = "num:" + obj.num + ",state:0"
      // console.log(ctrl)
      // console.log(model.deviceId)
       
      util.ctrlControl(model.deviceId,ctrl,function(success,msg){
          if (!success) {     
            console.log(msg)
          }
       })




		this.setData({
          KgOn:false,
          isBF: false,
          bfTitle: "撤"
       })
		 
	},
  SWDtouchMove: function(e){


     var x = e.changedTouches[0].pageX
     var y = e.changedTouches[0].pageY
     var movePoint = {x:x,y:y} 
     var center = {x:(this.data.SCREEN_WIDTH/2),y:290}
     // var currentAngle = tool.AngleFromNorth(center.x,center.y,movePoint.x,movePoint.y)
     // var an =  this.data.rotateAngle + currentAngle
          
      
    
var currentAngle = tool.angleBetweenThreePoints(center.x,center.y,this.data.startPoint.x,this.data.startPoint.y,movePoint.x,movePoint.y)
var an = this.data.rotateAngle + currentAngle



 console.log(an) 

     this.setData({
      startPoint:movePoint,
      rotateAngle: an
     })
       
  },
  SWDtouchstart:function(e){
   // console.log('state-swd')
    //得到触摸点的坐标
      var x = e.changedTouches[0].pageX
      var y = e.changedTouches[0].pageY

      // console.log(e)
      // console.log('touchstart:x:'+ x +'y:' +y)
      var startPoint = {x:x,y:y}
      this.setData({
        startPoint:startPoint
      })
     
  },
  SWDtouchend:function(e){
      //得到触摸点的坐标
      var x = e.changedTouches[0].pageX
      var y = e.changedTouches[0].pageY
  },
  SWDtapKg:function(e){
    console.log('SWDtapKg')
     wx.showToast({
       title: "tap",
       icon: "",//仅支持success或者loading
       duration: 2000,
       success: function(res) {
         
       },
       fail: function (res) {
         
       },
       complete: function (res) {
         
       }
     });

  },
  tapArrow:function(e){
  var self = this
  var  isShowBottom = !(self.data.isShowBottom)
  var  arrowRotate = self.data.arrowRotate + 180

  
 

  if (!isShowBottom) {
      
     this.animation.translateY(0).step()

  }else{

     this.animation.translateY( -(this.data.listHeight)/2).step()
  }

  
 
 self.setData({
  isShowBottom:isShowBottom,
  arrowRotate:arrowRotate,
  animationData:this.animation.export()
 })


  },
  bgTouchMove:function(e){
    //进度条背景
   console.log(e)
  },
  bgTouchStart:function(e){
    console.log('bgTouchStart')
  },
  bgTouchEnd:function(e){
     console.log('bgTouchEnd')
  }




})	



